package com.henry.c05;

import com.henry.c05.autoconfig.DefaultClass;
import com.henry.c05.model.ISchool;
import com.henry.c05.model.Klass;
import com.henry.c05.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class C05AopApplication {


    public static void main(String[] args) throws Throwable {
        question1to3();
    }
    //作业1-3题
    private static void question1to3() throws Throwable{

        //加载配置
        ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student =(Student) context.getBean("student01");
        System.out.println(student.toString());

        Klass klass1=(Klass)context.getBean("klass01") ;
        //Klass klass1=context.getBean(Klass.class) ; //如果在xml中配置了两个bean,且都引用klass，则此方法会报错。

        //调用下面方法，会被Aop拦截，此Aop是使用代码的方式实现，具体实现方式为aop.MyAop
        klass1.print();

        //调用下面方法，会被Aop拦截，此Aop使用代码的方式实现，具体实现方式为aop.MyAop以及resource下applicationContext.xml中的<aop:config>配置
        klass1.print1();

        //利用Aop后，实际类型为EnhancerBySpringCGLIB
        System.out.println("Klass对象AOP代理后的实际类型："+klass1.getClass());
        System.out.println("Klass对象AOP代理后的实际类型是否是Klass子类："+(klass1 instanceof Klass));
        //通过自动装配
        //ISchool school1 =(ISchool) context.getBean(ISchool.class);
        ISchool school1 =(ISchool) context.getBean("school1");
        school1.ding();

        //通过Xml配置
        ISchool school2 =(ISchool) context.getBean("school2");
        school2.ding();
        //利用Aop代理后，实际类型为com.sun.proxy.$Proxy1，若类型想为EnhancerBySpringCGLIB，则需要在xml配置中设置proxy-target-class为true <aop:aspectj-autoproxy  proxy-target-class="true" />
        System.out.println("school对象AOP代理后的实际类型："+school1.getClass());
    }


}
